package cn.com.dhc.danlu.widget.home;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiji.www.frameworks.utils.DensityUtils;

import java.math.BigDecimal;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.dhc.danlu.widget.adapter.GeneralGoodsAdapter;
import cn.com.hd.mall.web.webservices.entity.response.merch.base.BaseMerchListModel;

/**
 * User: Geek_Soledad(wuhaiyang@danlu.com)
 * Date: 2016-08-04
 * Time: 20:21
 * Description: 封装商品信息栏
 * GeneralGoodsAdapter use
 */
public class GoodsInfoHolder {
    // Content View Elements
    private CornerImageView mCiv_good_pic;
    private TextView mTv_product_name;
    private TextView mTv_promotion;
    private TextView mTv_goods_price;
    private ImageView mIv_cuxiao;
    private ImageView mIv_jifen;
    private ImageView mIv_hbao;
    private JoinShopCarButton mJscb_car;
    private TextView mTv_company;
    private View dot_line;
    private View divider;

    public GoodsInfoHolder (View parentView) {
        initView (parentView);
    }

    public void refreshView (BaseMerchListModel model, GeneralGoodsAdapter.ListType listType, final OnShopClickListener shopCarListener) {
        String url = model.getPicUrl ();
        mCiv_good_pic.loadImg (url);
        mCiv_good_pic.setRightState (model.getIsPromotion());
        //首页推荐栏位不显示推荐标志
        if (listType == GeneralGoodsAdapter.ListType.recommended) {
            mCiv_good_pic.setLeftState("1");
        } else {
            mCiv_good_pic.setLeftState (model.getIsRecommended());
        }
        if (!TextUtils.isEmpty (model.getMerchName ())) {
            mTv_product_name.setText(model.getMerchName());
            mTv_product_name.setBackgroundColor(Color.TRANSPARENT);
            mTv_promotion.setText (model.getGoodsPromotionInfo());
            mTv_promotion.setBackgroundColor (Color.TRANSPARENT);
        }

        //外部activity 调用
        mTv_product_name.setTag (model.getMerchId ());

        String res = null;
        String money;
        int targetIndex = 0;

        if (!TextUtils.isEmpty (model.getDanluPrice ())) {
            money = CommonUtils.fenToYuan (new BigDecimal (model.getDanluPrice ())).toPlainString ();
            res = "¥ " + money + "/" + model.getMerchSpec ();
            targetIndex = ("¥ " + money).length ();
            mTv_goods_price.setText (getSpannable (res, targetIndex, getContext ().getResources ().getColor (R.color.color_b12525)));
        }
        String isPromotion = model.getIsPromotion ();
        if (isPromotion.equals ("0")) {
            mIv_cuxiao.setVisibility (View.VISIBLE);
        } else {
            mIv_cuxiao.setVisibility (View.GONE);
        }

        if (listType == GeneralGoodsAdapter.ListType.often_buy_off) {
            mJscb_car.setVisibility (View.GONE);
            mTv_promotion.setTextColor (Color.BLACK);
            mTv_goods_price.setText (getSpannable (res, targetIndex, Color.BLACK));
        } else {
            if (!TextUtils.isEmpty (model.getMerchStatus ())) {
                mJscb_car.setVisibility (View.VISIBLE);
                mJscb_car.setMerchState (model.getMerchStatus ());
                //设置购物车监听时间
//                mJscb_car.setOnClickListener (shopCarListener);
                mJscb_car.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (null != shopCarListener) {
                            shopCarListener.onClick(v,mJscb_car.getRawY());
                        }
                    }
                });
            }
        }
        mTv_company.setText (model.getSellerName ());

        // 根据是否存在促销信息展示虚线
        if (null == model.getPromotionList () || model.getPromotionList ().isEmpty ()) {
            dot_line.setVisibility (View.GONE);
        } else {
            dot_line.setVisibility (View.VISIBLE);
        }

    }

    private Spannable getSpannable (String text, int endIndex, int textColor) {
        Spannable textSpan = new SpannableStringBuilder (text);

        StyleSpan boldSpan = new StyleSpan (Typeface.BOLD);
        AbsoluteSizeSpan smallSizeSpan = new AbsoluteSizeSpan (DensityUtils.dip2px (getContext (), 12));
        AbsoluteSizeSpan middleSizeSpan = new AbsoluteSizeSpan (DensityUtils.dip2px (getContext (), 14));
        AbsoluteSizeSpan largeSizeSpan = new AbsoluteSizeSpan (DensityUtils.dip2px (getContext (), 18));
        ForegroundColorSpan redSpan = new ForegroundColorSpan (textColor);

        textSpan.setSpan (boldSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textSpan.setSpan (smallSizeSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textSpan.setSpan (redSpan, 0, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textSpan.setSpan (boldSpan, 2, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textSpan.setSpan (largeSizeSpan, 2, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textSpan.setSpan (middleSizeSpan, endIndex, text.length (), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        return textSpan;
    }

    public Context getContext () {
        return mCiv_good_pic.getContext ();
    }

    public void initView (View view) {
        mCiv_good_pic = (cn.com.dhc.danlu.widget.home.CornerImageView) view.findViewById (R.id.civ_good_pic);
        mTv_product_name = (TextView) view.findViewById (R.id.tv_product_name);
        mTv_promotion = (TextView) view.findViewById (R.id.tv_promotion);
        mTv_goods_price = (TextView) view.findViewById (R.id.tv_goods_price);
        mIv_cuxiao = (ImageView) view.findViewById (R.id.iv_cuxiao);
        mIv_jifen = (ImageView) view.findViewById (R.id.iv_jifen);
        mIv_hbao = (ImageView) view.findViewById (R.id.iv_hbao);
        mJscb_car = (JoinShopCarButton) view.findViewById (R.id.jscb_car);
        mTv_company = (TextView) view.findViewById (R.id.tv_company);
        dot_line = view.findViewById (R.id.dot_line);
        divider = view.findViewById (R.id.divider);
    }
    public interface OnShopClickListener{
        void onClick(View view,float rawY);
    }
}
